﻿/**
 * research_popup 과 동일한 방식으로 화면 구성을 하게 되며
 * confirm() 함수를 통해서
 * DB에 완료된 설문 조사 데이타를 넘기게 된다.
 */

var _this = this;


$(document).ready(function() {
	var rId = $('#rId').attr('value');
	$('#confirm').click(confirm);
	fillData(rId);
//	_this.table1 = tableSync('tbl1');
	
});

function confirm(){

//	if(checkAllRadio()){
//		pezAlert("답변이 완료되지 않은 항목이 있습니다.");
//		return;
//	}
	
	var param=getData();
	logs(JSON.stringify(param));
	logs(11111);
	var sendParam = {'data':JSON.stringify(param)};
	var url = 'action/saveResearchData.json';
	$.post(url, sendParam, function(){
		
	});
	
}

function getData(){
	var param={};
	$('#tbl1').find('#subTbl').each(function (index) {
		var subParam={};
		var table = this;
		var answerType = $(table).find('#answerToggle').attr('value');
		subParam['qSeq']=$(table).find('#qSeq').attr('value');
		subParam['qType']=answerType;
		var value;
		switch(answerType){
		case '0':{//radio
			value =$(table).find('input:radio:checked').val();
//						
			break;
		}
		case '1':{//writing
			value = $(table).find('#writing').children().attr('value');
			
			break;
		}
		}
		subParam['answer']=value;
		subParam['pSeq']=_this.pSeq+"";
		param[index]=subParam;
	});
	return param;
}

function checkAllRadio(){
	var retFlag = false;
	$('#tbl1').find('#subTbl').each(function () {
		var table = this;
		logs(table);
		var answerType = $(table).find('#answerToggle').attr('value');
		logs('answerType ='+answerType );
		switch(answerType){
		case '0':{//radio
			var val =$(table).find('input:radio:checked').val();
//			logs('val = '+val);
			if(val == undefined){
				retFlag = true;
				return 1;	// break;	
			}
			
			break;
		}
		case '1':{//writing
			var val = $(table).find('#writing').children().attr('value');
			
			logs('writing val = '+val+', length = '+val.length);
			if(val.length == 0){
				retFlag = true;
				return 1;	// break;	
			}
			break;
		}
		}
	});
	
	return retFlag;
}














function fillData(rId){
	var url = 'action/getResearchData.json';
	var param={'rId':rId};
	logs('fillData rId='+rId);
	$.post(url, param, function(dat){
		_this.dat = jQuery.extend(true, {}, dat.dat);
		
		
//		logs(JSON.stringify(dat.dat));
		_this.pSeq = dat.dat.main.seq;
		logs('_this.pSeq = '+_this.pSeq);
//		_this.table1.tableAutoSync(dat.dat.main);
//		logs(JSON.stringify(dat.dat.main));
		makeSync(dat.dat);
		$('#wrap').show();
	});
}

function makeSync(dat){
	
//	logs(JSON.stringify(dat.main));
	$('#rContent').setterInput(dat.main.rContent);
	logs('length = '+dat.list_question.length);
	copyTrTag(dat.list_question);
	fillTableData(dat);
}

function fillTableData(dat){
	var body = $('#tbl1 > tbody');
	var q_list = dat.list_question;
	var a_list = dat.list_answer;
	
	logs('fillTableData');
//	logs(JSON.stringify(q_list));
	logs('11111111');
	$(body).children().each(function(index){
		var tr = this;
//		logs(tr);
		$(tr).find('#qSeq').setterInput(q_list[index].qSeq);
		$(tr).find('#qContent').setterInput(q_list[index].qContext);
		if(q_list[index].answerToggle==1){
			$(tr).find('#answerToggle').setterInput(1);
			$(tr).answerToggle(1);
		}
		copyDivAnswer(tr, a_list);
	});
	
	try{
		a_list = jQuery.extend(true, {}, _this.dat.list_answer);
	}catch(e){
		errLog('error ');
	}
	logs('=======================');
	$(body).children().each(function(index){
		var tr = this;
//		logs(tr);
		fillAnswerData($(tr).find('#radio_area'), $(tr).find('#qSeq').attr('value'),  a_list);
	});
}

function fillAnswerData(tr, valueNumber, a_list){
	var string = valueNumber;
	logs('fillAnswerData string  = '+string);

	var count=0;
	for(a in a_list){
		var item = a_list[a];
//		logs('item.qSeq = '+item.qSeq);
		
		if(item.qSeq==string){
			$(tr).find('input').each(function(index){
				index = index - index%2;
				logs('index = '+index);
//				logs($(this));
				logs('count = '+count+', index/2 = '+index/2);
				if(count == index/2){
					var name;
					var value;
					if($(this).attr('type')=='radio'){
						name = 'ansRadioId'+valueNumber;
						value = index/2;
					}else{
						name = string;
						value = item.aContext;
					};
					$(this).attr('id', name);
					$(this).attr('name', name);
					$(this).attr('value', value);	
				}
			});
			count++;
		}
	}
	
	removeItemInJson(string, a_list);
}

function copyDivAnswer(tr, a_list){
	var valueNumber = $(tr).find('#qSeq').attr('value');
	var string = valueNumber;
	var count = 0;
	
	logs('copyDivAnswer');
//	logs(JSON.stringify(a_list));
	
	
	for(var i=0;i<a_list.length;i++){
//		logs('a_list[i].qSeq = '+a_list[i].qSeq);
		if(a_list[i].qSeq==valueNumber){
			count++;
		}
	}
	logs('valueNumber = '+valueNumber);
	logs('count = '+count);
	var body = $(tr).find('#radio_area');
	logs(body);
	var html = $(body).first().html();
	logs(html);
	while(--count){
		$(body).append(html);
	}
	removeItemInJson(string, a_list);
	
	
}

function removeItemInJson(string, a_list){
	logs('removeItemInJson string = '+string);
	logs(typeof a_list.length);
	if(typeof a_list.length == 'undefined'){
		for(a in a_list){
			try{
//				logs('ori = '+a_list[a].aSeq);
				if(a_list[a].qSeq==string){
					delete a_list[a];
					removeItemInJson(string, a_list);
					break;
				}	
			}catch(e){
				logs(e);
			}
		}
	}else{
//		logs('removeItemInJson list length = '+a_list.length);
		for(var i=0;i<a_list.length;i++){
			try{
//			logs('ori = '+a_list[i].qSeq);
			if(a_list[i].qSeq==string){
				a_list.splice(i,1);
				removeItemInJson(string, a_list);
				break;
			}
			}catch(e){
				logs(e);
			}
		}	
	}
	
}


jQuery.fn.answerToggle = function( val){
	logs('answerToggle val = '+val);
	if(val==0){
		$(this).find('#multi').show();
		$(this).find('#writing').hide();
	}else if(val==1){
		$(this).find('#multi').hide();
		$(this).find('#writing').show();
	}
};

function copyTrTag(list){
	var length = list.length;
	var body = $('#tbl1 > tbody');
	var trHtml = '<tr></tr>';
	trHtml = $(body).children().last().html();
	
	while(--length){
		$(body).append('<tr>'+trHtml+'</tr>');
	}
}

jQuery.fn.setterInput = function( val){
	
	$(this).attr('value', val);
//	logs($(this));
	logs('setterInput 112 id = '+', value = '+$(this).attr('value'));
};

function errLog(e){
	console.log(e);
}

function logs(e){
	errLog(e);
}





